﻿namespace WMS6.Web.VAS
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.VASServiceRef;

    public class Operation : BasePage
    {
        protected Button btnCopy;
        protected Button btnDelete;
        protected Button btnGoBack;
        protected Button btnNew;
        protected Button btnSave;
        protected CheckBox chkIsActive;
        protected DropDownList ddlOperationType;
        protected HtmlForm F79731;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnOperationID;
        protected HtmlHead Head1;
        protected Label lblDesc;
        protected Label lblMsg;
        protected Label lblOperationID;
        protected Label lblOperationType;
        protected Label lblOwner;
        protected HtmlLink mycss;
        protected Label one1;
        protected ScriptManager ScriptManager1;
        protected TextBox txtDescr;
        protected TextBox txtOperationID;
        protected UpdatePanel up1;

        protected void btnCopy_Click(object sender, EventArgs e)
        {
            this.txtOperationID.Text = "";
            this.txtOperationID.Enabled = true;
            base.DataEntityViewState = null;
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.txtOperationID.Text.Trim() != "")
                {
                    List<string> OperationIDs = new List<string> {
                        this.txtOperationID.Text.Trim()
                    };
                    this.lblMsg.Text = VASServiceClientProxy.GetInstance().DeleteOperationInfo(OperationIDs) + base.GetGlobalMsgResource("C006");
                    this.btnSave.Enabled = false;
                    this.btnDelete.Enabled = false;
                    this.btnCopy.Enabled = false;
                    base.DataEntityViewState = null;
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C007") + ex.Message;
            }
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            switch (this.hdnFrom.Value)
            {
                case "query":
                    base.Response.Redirect("OperationList.aspx", false);
                    break;

                case "list":
                    base.Response.Redirect("OperationList.aspx?from=edit", false);
                    break;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtOperationID.Text))
            {
                this.lblMsg.Text = "请输入工序!";
            }
            else if (base.DataEntityViewState != null)
            {
                this.UpdateOperation();
            }
            else
            {
                this.InsertOperation();
            }
        }

        private OperationInfo GetOperation(OperationInfo operationInfo)
        {
            operationInfo.OperationID = this.txtOperationID.Text.Trim();
            operationInfo.OperationType = this.ddlOperationType.SelectedValue;
            operationInfo.Descr = this.txtDescr.Text.Trim();
            operationInfo.IsActive = this.chkIsActive.Checked ? "Y" : "N";
            return operationInfo;
        }

        private void InsertOperation()
        {
            try
            {
                OperationInfo operationInfo = new OperationInfo();
                operationInfo = this.GetOperation(operationInfo);
                operationInfo.CreatedBy = base.UserInfo.UserID;
                this.lblMsg.Text = VASServiceClientProxy.GetInstance().InsertOperationInfo(operationInfo) + base.GetGlobalMsgResource("C004");
                base.DataEntityViewState = operationInfo;
                this.txtOperationID.Enabled = false;
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C005") + ex.Message;
            }
        }

        private void LoadData()
        {
            base.BindData2DDLBySysCode(this.ddlOperationType, "OperationType ");
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.hdnOperationID.Value = base.Request.QueryString["ID"].ToString();
                this.SetOperation2From();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetOperation2From()
        {
            try
            {
                string operationID = this.hdnOperationID.Value;
                OperationInfo operation = VASServiceClientProxy.GetInstance().GetOperationInfoByID(operationID);
                base.DataEntityViewState = operation;
                if (operation != null)
                {
                    this.txtOperationID.Text = operation.OperationID;
                    this.txtOperationID.Enabled = false;
                    this.txtDescr.Text = operation.Descr;
                    this.ddlOperationType.SelectedValue = operation.OperationType;
                    this.chkIsActive.Checked = operation.IsActive == "Y";
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void UpdateOperation()
        {
            try
            {
                OperationInfo operationInfo = base.DataEntityViewState as OperationInfo;
                operationInfo = this.GetOperation(operationInfo);
                operationInfo.UpdatedBy = base.UserInfo.UserID;
                this.lblMsg.Text = VASServiceClientProxy.GetInstance().UpdateOperationInfo(operationInfo) + base.GetGlobalMsgResource("C002");
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C003") + ex.Message;
            }
        }
    }
}

